package com.tongbo.mobilesafe.service;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;
import com.tongbo.mobilesafe.R;
import com.tongbo.mobilesafe.db.dao.BlackNumberDao;
import com.tongbo.mobilesafe.engine.NumberAddressService;
import com.tongbo.mobilesafe.ui.CallSMSActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.provider.CallLog;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AddressService extends Service {
	private static final String TAG = "AddressService";
	private TelephonyManager manager;
	private MyPhonelistener listener;
	private WindowManager windowmanager;
	private View view;
	private SharedPreferences sp;
	private BlackNumberDao dao;
	private long firstRingTime;//铃声开始时间
	private long endRingTime;//铃声结束时间
	@Override
	public void onCreate() {
		super.onCreate();
		dao = new BlackNumberDao(this);
		listener = new MyPhonelistener();
		windowmanager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		// 注册系统电话管理服务的监听器
		manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
	}

	private class MyPhonelistener extends PhoneStateListener {
		// 电话状态发生改变的时候调用的方法
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			super.onCallStateChanged(state, incomingNumber);
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE:// 静止状态，没有呼叫
				endRingTime = System.currentTimeMillis();
				if(endRingTime>firstRingTime && endRingTime-firstRingTime<2000){
					Log.i(TAG, "响一声电话");
					firstRingTime = 0;
					endRingTime = 0;
					//弹出来notification通知用户这是一个骚扰电话
					showNotification(incomingNumber);
				}
				if (view != null) {
					windowmanager.removeView(view);
					view = null;
				}
				// 再获取一次系统的时间
				break;
			case TelephonyManager.CALL_STATE_RINGING:// 铃响状态
				firstRingTime = System.currentTimeMillis();
				Log.i(TAG, "手机号码为:" + incomingNumber);
				//判断incomingnumber是否在黑名单内
				if(dao.find(incomingNumber)){
					//挂断电话
					Log.i(TAG, "挂断电话");
					endCall();
					//deleteCallLog(incomingNumber);
					//注册一个内容观察者 观察call_log的uri的信息
					getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI, true, new MyObserver(new Handler(), incomingNumber));
				}
				
				String address = NumberAddressService.getAddress(incomingNumber);
				Log.i(TAG, "号码归属地为:" + address);
				// Toast.makeText(getApplicationContext(), "号码归属地为:"+address, 1).show();
				showLocation(address);
				//获取一下当前系统时间
				
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:// 接通电话状态
				if (view != null) {
					windowmanager.removeView(view);
					view = null;
				}
				break;
			}
		}
	}
	/**
	 * 弹出notification通知用户添加黑名单号码
	 * @param incomingNumber
	 */
	@SuppressWarnings("deprecation")
	private void showNotification(String incomingNumber) {
		//1、获取notification管理服务
		NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		//2、创建notification对象并显示
		int icon =R.drawable.notification;
		CharSequence tickerText = "发现响一声号码";
		long when = System.currentTimeMillis();
		
		Notification notification = new Notification(icon, tickerText, when);
		//3、配置notification的一些参数
		Context context = getApplicationContext();
		CharSequence contentTitle = "响一声号码";
		CharSequence contentText = incomingNumber;
		
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		Intent notificationIntent = new Intent(this, CallSMSActivity.class);
		//把响一声的号码设置到intent对象里面
		notificationIntent.putExtra("number", incomingNumber);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	/**
	 * 根据电话号码，删除呼叫记录
	 * @param incomingNumber 要删除的呼叫记录号码
	 */
	public void deleteCallLog(String incomingNumber) {
		ContentResolver resolver = getContentResolver();
		Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null, "number=?", new String[]{incomingNumber}, null);
		if(cursor.moveToFirst()){//查询到了呼叫记录
			String id = cursor.getString(cursor.getColumnIndex("_id"));
			resolver.delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[]{id});
		}
	}
	/**
	 * 系统隐藏的方法 挂断电话
	 */
	public void endCall() {
		try {
			Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
			IBinder binder = (IBinder)method.invoke(null, new Object[]{TELEPHONY_SERVICE});
			ITelephony telephony = ITelephony.Stub.asInterface(binder);
			telephony.endCall();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		manager.listen(listener, PhoneStateListener.LISTEN_NONE);
		listener = null;
	}

	/**
	 * 窗体上显示位置信息
	 * 
	 * @param address
	 *            位置信息
	 */
	public void showLocation(String address) {
		WindowManager.LayoutParams params = new LayoutParams();
		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		params.width = WindowManager.LayoutParams.WRAP_CONTENT;
		params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
		params.format = PixelFormat.TRANSLUCENT;
		params.type = WindowManager.LayoutParams.TYPE_TOAST;
		params.setTitle("Toast");
		params.gravity = Gravity.LEFT | Gravity.TOP;

		params.x = sp.getInt("lastx", 0);
		params.y = sp.getInt("lasty", 0);

		view = View.inflate(getApplicationContext(), com.tongbo.mobilesafe.R.layout.show_location, null);
		LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_location);

		int backgroundid = sp.getInt("background", 0);
		if (backgroundid == 0) {
			ll.setBackgroundResource(R.drawable.call_locate_gray);
		} else if (backgroundid == 1) {
			ll.setBackgroundResource(R.drawable.call_locate_orange);
		} else {
			ll.setBackgroundResource(R.drawable.call_locate_green);
		}

		TextView tv = (TextView) view.findViewById(R.id.tv_location);
		tv.setTextSize(24);
		tv.setText(address);
		windowmanager.addView(view, params);
	}
	private class MyObserver extends ContentObserver{
		private String incomingNumber;
		public MyObserver(Handler handler,String incomingNumber) {
			super(handler);
			this.incomingNumber = incomingNumber;
		}

		@Override
		public void onChange(boolean selfChange) {
			super.onChange(selfChange);
			deleteCallLog(incomingNumber);
			//当删除了电话号码后反注册内容观察者
			getContentResolver().unregisterContentObserver(this);
		}
		
	}
}
